﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Windows.Media;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class VisualCollection<T> : IEnumerable<T> where T: Visual
    {
        private Visual _parent;
        private List<T> _shadowCopy = new List<T>();

        public VisualCollection(Visual parent)
        {
            Contract.Requires(parent != null);
            this._parent = parent;
            _items = new VisualCollection(_parent);
        }
        VisualCollection _items;

        public int Count
        {
            get { return _items.Count; }
        }

        public IEnumerator<T> GetEnumerator()
        {
            foreach (T item in _shadowCopy)
                yield return item;
        }

        public T this[int index]
        {
            get
            {
#if DEBUG
            Contract.Assert(Consistent);
#endif
                return (T)_shadowCopy[index];
            }
        }

        public void Add(T item)
        {
#if DEBUG
            Contract.Assert(Consistent);
#endif
            _items.Add(item);
            _shadowCopy.Add(item);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Clear()
        {
#if DEBUG
            Contract.Assert(Consistent);
#endif
            _items.Clear();
            _shadowCopy.Clear();
        }

        public bool Contains(T element)
        {
#if DEBUG
            Contract.Assert(Consistent);
#endif
            return _shadowCopy.Contains(element);
        }

        public void Remove(T element)
        {
#if DEBUG
            Contract.Assert(Consistent);
#endif
            _items.Remove(element);
            _shadowCopy.Remove(element);
        }
#if DEBUG
        bool Consistent
        {
get
        {
return _shadowCopy.Count == _items.Count;}}
#endif
    }
}